using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace DD.Data.SQL
{
    public static class ImageManager
    {
        public static void AddImageToProduct(int pid, string filename)
        {

            SqlConnection conn = new SqlConnection();
            conn = DD.Data.SQL.DataManager.GetConnection();

            SqlCommand com = new SqlCommand("sp_DD_ADD_IMAGE_TO_PRODUCT", conn);
            com.CommandType = CommandType.StoredProcedure;

            SqlParameter param1 = new SqlParameter("@PID", pid);
            param1.SqlDbType = SqlDbType.Int;

            SqlParameter param2 = new SqlParameter("@FILENAME", filename);
            param2.SqlDbType = SqlDbType.NVarChar;

            com.Parameters.Add(param1);
            com.Parameters.Add(param2);

            conn.Open();
            com.ExecuteNonQuery();
            conn.Close();

        }

        public static DataSet GetProductImages(int pid)
        {
            SqlConnection conn = new SqlConnection();
            conn = DD.Data.SQL.DataManager.GetConnection();

            SqlCommand com = new SqlCommand("sp_DD_GET_PRODUCT_IMAGES", conn);
            com.CommandType = CommandType.StoredProcedure;

            SqlParameter param1 = new SqlParameter("@PID", pid);
            param1.SqlDbType = SqlDbType.Int;

            com.Parameters.Add(param1);

            SqlDataAdapter da = new SqlDataAdapter(com);
            DataSet ds = new DataSet();

            da.Fill(ds);

            return ds;


        }

        public static string GetProductDefaultImagePath(int pid)
        {
            SqlConnection conn = new SqlConnection();
            conn = DD.Data.SQL.DataManager.GetConnection();

            SqlCommand com = new SqlCommand("sp_DD_GET_DEFAULT_IMAGE_PATH", conn);
            com.CommandType = CommandType.StoredProcedure;

            SqlParameter param1 = new SqlParameter("@PID", pid);
            param1.SqlDbType = SqlDbType.Int;

            com.Parameters.Add(param1);

            string path = "";
            try
            {
                conn.Open();
                path = (string)com.ExecuteScalar();
                conn.Close();
            }
            catch
            { }


            return path;
        }

        public static int GetProductDefaultImageId(int pid)
        {
            SqlConnection conn = new SqlConnection();
            conn = DD.Data.SQL.DataManager.GetConnection();

            SqlCommand com = new SqlCommand("sp_DD_GET_DEFAULT_IMAGE_ID", conn);
            com.CommandType = CommandType.StoredProcedure;

            SqlParameter param1 = new SqlParameter("@PID", pid);
            param1.SqlDbType = SqlDbType.Int;

            com.Parameters.Add(param1);

            int id = 0;
            try
            {
                conn.Open();
                id = (int)com.ExecuteScalar();
                conn.Close();
            }
            catch
            { }


            return id;
        }

        public static string GetImagePath(int ImageId)
        {
            SqlConnection conn = new SqlConnection();
            conn = DD.Data.SQL.DataManager.GetConnection();

            SqlCommand com = new SqlCommand("sp_DD_GET_IMAGE_PATH", conn);
            com.CommandType = CommandType.StoredProcedure;

            SqlParameter param1 = new SqlParameter("@IMAGEID", ImageId);
            param1.SqlDbType = SqlDbType.Int;

            com.Parameters.Add(param1);

            string path = "";
            //try
            //{
            conn.Open();
                path = (string)com.ExecuteScalar();
                conn.Close();
            //}
            //catch
            //{ }


            return path;
        }
    }


}
